package com.codeoss.framework.context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.codeoss.framework.request.ViewRequest;
import com.codeoss.framework.view.clientscript.IClientScript;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Message;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.pagebean.base.BasePageBean;

public class YunContext {
	private Map<String, String> queryMap;
	private BasePageBean pageBean;
	private Page page;
	private boolean postBack;
	private boolean renderCompleted;
	private ViewRequest viewRequest;
	private Component findComponent;
	private Map<String,Object> attrMap=new HashMap<String,Object>();
	private Map<String,Object> varValueMap=new HashMap<String,Object>();
	private List<IClientScript> clientScriptList=new ArrayList<IClientScript>();
	private List<Message> messageList=new ArrayList<Message>();
	private Object currentId;
	private String masterModel;
	private boolean validationEnabled;
	private ExternalContext externalContext;
	private String token;
	private Map<String,Object> pageParaMap=new HashMap<String,Object>();
	private List<Component> renderComponentList;
	
	private static ThreadLocal<YunContext> threadLocal = new ThreadLocal<YunContext>() {
		protected YunContext initialValue() {
			return null;
		}
	};

	public static YunContext getCurrentContext() {
		return threadLocal.get();
	}

	public static void setCurrentContext(YunContext yunContext) {
		threadLocal.set(yunContext);
	}

	public static void dispose() {
		threadLocal.remove();
	}
	
	public BasePageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(BasePageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public boolean isPostBack() {
		return postBack;
	}

	public void setPostBack(boolean postBack) {
		this.postBack = postBack;
	}
	
	public boolean isRenderCompleted() {
		return renderCompleted;
	}

	public void setRenderCompleted(boolean renderCompleted) {
		this.renderCompleted = renderCompleted;
	}
	
	public void setAttribute(String key,Object value) {
		attrMap.put(key, value);
	}
	
	public <T extends Object> T getAttribute(String key) {
		return (T)attrMap.get(key);
	}
	
	public Object removeAttribute(String key) {
		return attrMap.remove(key);
	}

	public boolean containAttribute(String key) {
		return attrMap.containsKey(key);
	}
	
	public Map<String, String> getQueryMap() {
		return queryMap;
	}

	public void setQueryMap(Map<String, String> queryMap) {
		this.queryMap = queryMap;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public Component getFindComponent() {
		return findComponent;
	}

	public void setFindComponent(Component findComponent) {
		this.findComponent = findComponent;
	}

	public Map<String, Object> getVarValueMap() {
		return varValueMap;
	}

	public void setVarValueMap(Map<String, Object> varValueMap) {
		this.varValueMap = varValueMap;
	}
	
	public List<IClientScript> getClientScriptList() {
		return clientScriptList;
	}

	public void addClientScript(IClientScript clientScript) {
		clientScriptList.add(clientScript);
	}
	
	public List<Message> getMessageList() {
		return messageList;
	}

	public void addMessage(Message message) {
		messageList.add(message);
	}

	public Object getCurrentId() {
		return currentId;
	}

	public void setCurrentId(Object currentId) {
		this.currentId = currentId;
	}
	
	public boolean isValidationEnabled() {
		return validationEnabled;
	}

	public void setValidationEnabled(boolean validationEnabled) {
		this.validationEnabled = validationEnabled;
	}

	public ExternalContext getExternalContext() {
		return externalContext;
	}

	public void setExternalContext(ExternalContext externalContext) {
		this.externalContext = externalContext;
	}

	public String getMasterModel() {
		return masterModel;
	}

	public void setMasterModel(String masterModel) {
		this.masterModel = masterModel;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}
	
	public Map<String, Object> getPageParaMap() {
		return pageParaMap;
	}

	public void setPageParaMap(Map<String, Object> pageParaMap) {
		this.pageParaMap = pageParaMap;
	}

	public void addPagePara(String key,Object value) {
		pageParaMap.put(key, value);
	}

	public List<Component> getRenderComponentList() {
		return renderComponentList;
	}

	public void setRenderComponentList(List<Component> renderComponentList) {
		this.renderComponentList = renderComponentList;
	}
}
